using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System;
	using System.Web.Mvc;
	using System.Web.Security;
	using System.Collections.Generic;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using TemplateWeb.Models;
	using System.IO;
	using System.Web;
    using System.Security;
	#endregion
	public abstract class BaseController : Controller
	{
        protected ThongTinCongTyRepository _repThongTinCongTy = new ThongTinCongTyRepository();
		protected LoaiHinhThamDinhGiaRepository _repLoaiHinhThamDinhGia = new LoaiHinhThamDinhGiaRepository();
		protected CongTyThamDinhGiaRepository _repCongTyThamDinhGia = new CongTyThamDinhGiaRepository();
		protected PhapLyRepository _repPhapLy = new PhapLyRepository();
		protected PhanHoiRepository _repPhanHoi = new PhanHoiRepository();
		protected GopYRepository _repGopY = new GopYRepository();
		protected SLM_MembersRepository _repSLM_Members = new SLM_MembersRepository();
        protected HoTroRepository _repHoTro = new HoTroRepository();
        protected GioiThieuCongTyRepository _repGioiThieuCongTy = new GioiThieuCongTyRepository();

		protected string RenderPartialViewToString()
		{
			return RenderPartialViewToString(null, null);
		}

		protected string RenderPartialViewToString(string viewName)
		{
			return RenderPartialViewToString(viewName, null);
		}

		protected string RenderPartialViewToString(string viewName, object model)
		{
			if (string.IsNullOrEmpty(viewName))
				viewName = ControllerContext.RouteData.GetRequiredString("action");
			ViewData.Model = model;
			using (StringWriter sw = new StringWriter())
			{
				ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
				ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
				viewResult.View.Render(viewContext, sw);
				return sw.GetStringBuilder().ToString();
			}
		}


		public GroupViewModel CreateViewModel(DataTransferViewModel dataTransfer)
		{
			GroupViewModel viewModel = new GroupViewModel();
			if (dataTransfer.EnumViewModelType.ToString().Contains("ADMIN"))
			{
			}
			switch (dataTransfer.EnumViewModelType)
			{
				case EnumViewModel.ADMIN_DETAILOF_SLM_MEMBERS:
					break;
				case EnumViewModel.ADMIN_DETAILOF_CONGTYTHAMDINHGIA:
                    viewModel.CongTyThamDinhGiaModel = RetrieveDetailOfCongTyThamDinhGiaViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_DETAILOF_PHAPLY:
                    viewModel.PhapLyModel = RetrieveDetailOfPhapLyViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_DETAILOF_GOPY:
					break;
                case EnumViewModel.ADMIN_DETAILOF_LOAIHINHTHAMDINHGIA:
                    viewModel.LoaiHinhThamDinhGiaModel = RetrieveDetailOfLoaiHinhThamDinhGiaViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_DETAILOF_GIOITHIEUCONGTY:
                    viewModel.GioiThieuCongTyModel = RetrieveDetailOfGioiThieuCongTyViewModel(dataTransfer);
                    break;
				case EnumViewModel.ADMIN_SLM_MEMBERS:
                    viewModel.SLM_MembersModel = RetrieveSLM_MembersViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_LOAIHINHTHAMDINHGIA:
                    viewModel.LoaiHinhThamDinhGiaModel = RetrieveLoaiHinhThamDinhGiaViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_CONGTYTHAMDINHGIA:
                    viewModel.CongTyThamDinhGiaModel = RetrieveCongTyThamDinhGiaViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_PHAPLY:
                    viewModel.PhapLyModel = RetrievePhapLyViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_PHANHOI:
                    viewModel.PhanHoiModel = RetrievePhanHoiViewModel(dataTransfer);
                    viewModel.GopYModel = RetrieveDetaileOfGopYViewModel(dataTransfer);
					break;
				case EnumViewModel.ADMIN_GOPY:
                    viewModel.GopYModel = RetrieveGopYViewModel(dataTransfer);
					break;
                case EnumViewModel.ADMIN_HOTRO:
                    viewModel.HoTroModel = RetrieveHoTroViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_THONGTINCONGTY:
                    viewModel.ThongTinCongTyModel = RetrieveThongTinCongTyViewModel(dataTransfer);
                    break;
                case EnumViewModel.ADMIN_GIOITHIEUCONGTY:
                    viewModel.GioiThieuCongTyModel = RetrieveGioiThieuCongTyViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_INDEX:
                    viewModel.LoaiHinhThamDinhGiaModel = RetrieveLoaiHinhThamDinhGiaViewModel(dataTransfer);
                    viewModel.GioiThieuCongTyModel = RetrieveGioiThieuCongTyViewModel(dataTransfer);
                    viewModel.HoTroModel = RetrieveHoTroViewModel(dataTransfer);
                    viewModel.CongTyThamDinhGiaModel = RetrieveAllCongTyThamDinhGiaViewModel(dataTransfer);
                    break;
				case EnumViewModel.HOME_REGISTER:
					break;
				case EnumViewModel.HOME_DETAILOF_SLM_Members:
					break;
                case EnumViewModel.HOME_GIOITHIEU:
                    viewModel.LoaiHinhThamDinhGiaModel = RetrieveLoaiHinhThamDinhGiaViewModel(dataTransfer);
                    viewModel.GioiThieuCongTyModel = RetrieveGioiThieuCongTyViewModel(dataTransfer);
                    viewModel.HoTroModel = RetrieveHoTroViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_DANHSACHTHANHVIEN:
                    viewModel.GioiThieuCongTyModel = RetrieveGioiThieuCongTyViewModel(dataTransfer);
                    viewModel.LoaiHinhThamDinhGiaModel = RetrieveLoaiHinhThamDinhGiaViewModel(dataTransfer);
                    viewModel.HoTroModel = RetrieveHoTroViewModel(dataTransfer);
                    viewModel.CongTyThamDinhGiaModel = RetrieveAllCongTyThamDinhGiaViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_CHITIETCONGTY:
                    viewModel.GioiThieuCongTyModel = RetrieveGioiThieuCongTyViewModel(dataTransfer);
                    viewModel.LoaiHinhThamDinhGiaModel = RetrieveLoaiHinhThamDinhGiaViewModel(dataTransfer);
                    viewModel.HoTroModel = RetrieveHoTroViewModel(dataTransfer);
                    viewModel.CongTyThamDinhGiaModel = RetrieveDetailOfCongTyThamDinhGiaViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_PHAPLY:
                    viewModel.GioiThieuCongTyModel = RetrieveGioiThieuCongTyViewModel(dataTransfer);
                    viewModel.LoaiHinhThamDinhGiaModel = RetrieveLoaiHinhThamDinhGiaViewModel(dataTransfer);
                    viewModel.HoTroModel = RetrieveHoTroViewModel(dataTransfer);
                    viewModel.PhapLyModel = RetrievePhapLyViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_CHITIETPHAPLY:
                    viewModel.GioiThieuCongTyModel = RetrieveGioiThieuCongTyViewModel(dataTransfer);
                    viewModel.LoaiHinhThamDinhGiaModel = RetrieveLoaiHinhThamDinhGiaViewModel(dataTransfer);
                    viewModel.HoTroModel = RetrieveHoTroViewModel(dataTransfer);
                    viewModel.PhapLyModel = RetrieveDetailOfPhapLyViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_HOIDAP:
                    viewModel.GioiThieuCongTyModel = RetrieveGioiThieuCongTyViewModel(dataTransfer);
                    viewModel.LoaiHinhThamDinhGiaModel = RetrieveLoaiHinhThamDinhGiaViewModel(dataTransfer);
                    viewModel.HoTroModel = RetrieveHoTroViewModel(dataTransfer);
                    break;
                case EnumViewModel.HOME_LOAIHINHTHAMDINHGIA:
                    viewModel.GioiThieuCongTyModel = RetrieveGioiThieuCongTyViewModel(dataTransfer);
                    viewModel.LoaiHinhThamDinhGiaModel = RetrieveLoaiHinhThamDinhGiaViewModel(dataTransfer);
                    viewModel.HoTroModel = RetrieveHoTroViewModel(dataTransfer);
                    viewModel.DetailOfLoaiHinhThamDinhGiaModel = RetrieveDetailOfLoaiHinhThamDinhGiaViewModel(dataTransfer);
                    break;
			}
			return viewModel;
		}

		public bool UploadFile(HttpPostedFileBase file, string path, string name, ref string refName)
		{
			try
			{
				if (!System.IO.File.Exists(Server.MapPath(path + file.FileName)))
				{
					FileInfo f = new FileInfo(file.FileName);
					long ngay = DateTime.Now.ToBinary();
					refName = name + ngay + f.Extension;
					string fullPath = path + refName;
					if (file == null)
					return false;
					byte[] buffer = new byte[file.ContentLength];
					file.InputStream.Read(buffer, 0, file.ContentLength);
					FileStream newFile = new FileStream(Server.MapPath(fullPath), FileMode.Create, FileAccess.Write);
					newFile.Write(buffer, 0, file.ContentLength);
					newFile.Close();
				}
				else
				{
					refName = file.FileName;
				}
				return true;
			}
			catch
			{
				return false;
			}
		}

		public void DeleteFile(string filePath)
		{
			FileInfo file = new FileInfo(filePath);
			if (file.Exists)
			{
				file.Delete();
			}
		}

		public bool SendingMail(string subject, string emailContent, string to)
		{
			bool result = false;
			try
			{
                var thongtincongty = RetrieveThongTinCongTyViewModel(new DataTransferViewModel());
                if (thongtincongty.GetModel.LstObjModel.Count > 0)
                {
                    WebConfiguration.EmailLienHe = thongtincongty.GetModel.LstObjModel[0].EmailLienHe;
                    WebConfiguration.PasswordEmailLienHe = Encryption.Decrypt(thongtincongty.GetModel.LstObjModel[0].PasswordEmailLienHe);
                    emailContent = emailContent.Replace("[EmailLienHe]", WebConfiguration.EmailLienHe);

                    System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient();
                    System.Net.NetworkCredential auth = new System.Net.NetworkCredential(WebConfiguration.EmailLienHe, WebConfiguration.PasswordEmailLienHe);
                    mailclient.Host = WebConfiguration.MailServer;
                    mailclient.EnableSsl = true;
                    mailclient.UseDefaultCredentials = true;
                    mailclient.Credentials = auth;
                    System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(WebConfiguration.EmailLienHe, to);
                    mm.Subject = subject;
                    mm.IsBodyHtml = true;
                    mm.Body = emailContent;
                    mailclient.Send(mm);
                    result = true;
                }
			}
			catch
			{
				result = false;
			}
			return result;
		}

        public LoaiHinhThamDinhGiaViewModel RetrieveLoaiHinhThamDinhGiaViewModel(DataTransferViewModel dataTransfer)
        {
            return new LoaiHinhThamDinhGiaViewModel
            {
                GetModel = new GetLoaiHinhThamDinhGiaViewModel
                {
                    LstObjModel = _repLoaiHinhThamDinhGia.SelectAll(),
                    TotalItem = _repLoaiHinhThamDinhGia.GetTotalItem(),
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddLoaiHinhThamDinhGiaViewModel
                {
                },
                EditModel = new EditLoaiHinhThamDinhGiaViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public CongTyThamDinhGiaViewModel RetrieveAllCongTyThamDinhGiaViewModel(DataTransferViewModel dataTransfer)
        {
            return new CongTyThamDinhGiaViewModel
            {
                GetModel = new GetCongTyThamDinhGiaViewModel
                {
                    LstObjModel = _repCongTyThamDinhGia.SelectAll(),
                    TotalItem = _repCongTyThamDinhGia.GetTotalItem(),
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddCongTyThamDinhGiaViewModel
                {
                },
                EditModel = new EditCongTyThamDinhGiaViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public CongTyThamDinhGiaViewModel RetrieveCongTyThamDinhGiaViewModel(DataTransferViewModel dataTransfer)
        {
            return new CongTyThamDinhGiaViewModel
            {
                GetModel = new GetCongTyThamDinhGiaViewModel
                {
                    LstObjModel = _repCongTyThamDinhGia.SelectPaging(dataTransfer.CurrentPage, WebConfiguration.NumCongTyThamDinhGiaPerPage),
                    TotalItem = _repCongTyThamDinhGia.GetTotalItem(),
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddCongTyThamDinhGiaViewModel
                {
                },
                EditModel = new EditCongTyThamDinhGiaViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

		public PhapLyViewModel RetrievePhapLyViewModel(DataTransferViewModel dataTransfer)
		{
			return new PhapLyViewModel
			{
				GetModel = new GetPhapLyViewModel
				{
					LstObjModel = _repPhapLy.SelectPaging(dataTransfer.CurrentPage, WebConfiguration.NumPhapLyPerPage),
					TotalItem = _repPhapLy.GetTotalItem(),
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddPhapLyViewModel
				{
				},
				EditModel = new EditPhapLyViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}

		public PhanHoiViewModel RetrievePhanHoiViewModel(DataTransferViewModel dataTransfer)
		{
			List<PhanHoi> lst = null;
			int totalitem = -1;
			if (dataTransfer.IDGopY != -1)
			{
				lst = _repPhanHoi.SelectByIDGopY(dataTransfer.IDGopY);
				totalitem = _repPhanHoi.GetTotalItemByIDGopY(dataTransfer.IDGopY);
			}
			else if (dataTransfer.IDNguoiPhanHoi != -1)
			{
				lst = _repPhanHoi.SelectByIDNguoiPhanHoi(dataTransfer.IDNguoiPhanHoi);
				totalitem = _repPhanHoi.GetTotalItemByIDNguoiPhanHoi(dataTransfer.IDNguoiPhanHoi);
			}
			else
			{
				lst = _repPhanHoi.SelectPaging(dataTransfer.CurrentPage, WebConfiguration.NumPhanHoiPerPage);
				totalitem = _repPhanHoi.GetTotalItem();
			}
			return new PhanHoiViewModel
			{
				GetModel = new GetPhanHoiViewModel
				{
					LstObjModel = lst,
					TotalItem = totalitem,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddPhanHoiViewModel
				{
				},
				EditModel = new EditPhanHoiViewModel
				{
					ID = dataTransfer.IntID,
				},
				ReferKeys = new PhanHoiReferKeys()
				{
					IDGopY = dataTransfer.IDGopY,
					IDNguoiPhanHoi = dataTransfer.IDNguoiPhanHoi,
				},
				InfoText = dataTransfer.InfoText
			};
		}

		public GopYViewModel RetrieveGopYViewModel(DataTransferViewModel dataTransfer)
		{
			return new GopYViewModel
			{
				GetModel = new GetGopYViewModel
				{
					LstObjModel = _repGopY.SelectPaging(dataTransfer.CurrentPage, WebConfiguration.NumGopYPerPage),
					TotalItem = _repGopY.GetTotalItem(),
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddGopYViewModel
				{
				},
				EditModel = new EditGopYViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}


		public SLM_MembersViewModel RetrieveSLM_MembersViewModel(DataTransferViewModel dataTransfer)
		{
			return new SLM_MembersViewModel
			{
				GetModel = new GetSLM_MembersViewModel
				{
					LstObjModel = _repSLM_Members.SelectPaging(dataTransfer.CurrentPage, WebConfiguration.NumSLM_MembersPerPage),
					TotalItem = _repSLM_Members.GetTotalItem(),
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddSLM_MembersViewModel
				{
				},
				EditModel = new EditSLM_MembersViewModel
				{
					ID = dataTransfer.IntID,
				}
			};
		}

		public SLM_MembersViewModel RetrieveDetailOfSLM_MembersViewModel(DataTransferViewModel dataTransfer)
		{
			return new SLM_MembersViewModel
			{
				GetModel = new GetSLM_MembersViewModel
				{
					LstObjModel = _repSLM_Members.RetrieveByID(dataTransfer.IntID)
				},
				EditModel = new EditSLM_MembersViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}

        public GopYViewModel RetrieveDetaileOfGopYViewModel(DataTransferViewModel dataTransfer)
        {
            return new GopYViewModel
            {
                GetModel = new GetGopYViewModel
                {
                    LstObjModel = _repGopY.RetrieveByID(dataTransfer.IDGopY)
                },
                AddModel = new AddGopYViewModel
                {
                },
                EditModel = new EditGopYViewModel
                {
                    ID = dataTransfer.IDGopY,
                },
                InfoText = dataTransfer.InfoText
            };
        }

		public CongTyThamDinhGiaViewModel RetrieveDetailOfCongTyThamDinhGiaViewModel(DataTransferViewModel dataTransfer)
		{
			return new CongTyThamDinhGiaViewModel
			{
				GetModel = new GetCongTyThamDinhGiaViewModel
				{
					LstObjModel = _repCongTyThamDinhGia.RetrieveByID(dataTransfer.IntID)
				},
				EditModel = new EditCongTyThamDinhGiaViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}
		public PhapLyViewModel RetrieveDetailOfPhapLyViewModel(DataTransferViewModel dataTransfer)
		{
			return new PhapLyViewModel
			{
				GetModel = new GetPhapLyViewModel
				{
					LstObjModel = _repPhapLy.RetrieveByID(dataTransfer.IntID)
				},
				EditModel = new EditPhapLyViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}

        public HoTroViewModel RetrieveHoTroViewModel(DataTransferViewModel dataTransfer)
        {
            return new HoTroViewModel
            {
                GetModel = new GetHoTroViewModel
                {
                    LstObjModel = _repHoTro.SelectAll(),
                    TotalItem = _repHoTro.GetTotalItem(),
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddHoTroViewModel
                {
                },
                EditModel = new EditHoTroViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public ThongTinCongTyViewModel RetrieveThongTinCongTyViewModel(DataTransferViewModel dataTransfer)
        {
            return new ThongTinCongTyViewModel
            {
                GetModel = new GetThongTinCongTyViewModel
                {
                    LstObjModel = _repThongTinCongTy.SelectAll(),
                    TotalItem = _repThongTinCongTy.GetTotalItem(),
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddThongTinCongTyViewModel
                {
                },
                EditModel = new EditThongTinCongTyViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public LoaiHinhThamDinhGiaViewModel RetrieveDetailOfLoaiHinhThamDinhGiaViewModel(DataTransferViewModel dataTransfer)
        {
            return new LoaiHinhThamDinhGiaViewModel
            {
                GetModel = new GetLoaiHinhThamDinhGiaViewModel
                {
                    LstObjModel = _repLoaiHinhThamDinhGia.RetrieveByID(dataTransfer.IntID)
                },
                EditModel = new EditLoaiHinhThamDinhGiaViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public GioiThieuCongTyViewModel RetrieveGioiThieuCongTyViewModel(DataTransferViewModel dataTransfer)
        {
            return new GioiThieuCongTyViewModel
            {
                GetModel = new GetGioiThieuCongTyViewModel
                {
                    LstObjModel = _repGioiThieuCongTy.SelectAll(),
                    TotalItem = _repGioiThieuCongTy.GetTotalItem(),
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddGioiThieuCongTyViewModel
                {
                },
                EditModel = new EditGioiThieuCongTyViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public GioiThieuCongTyViewModel RetrieveDetailOfGioiThieuCongTyViewModel(DataTransferViewModel dataTransfer)
        {
            return new GioiThieuCongTyViewModel
            {
                GetModel = new GetGioiThieuCongTyViewModel
                {
                    LstObjModel = _repGioiThieuCongTy.RetrieveByID(dataTransfer.IntID)
                },
                EditModel = new EditGioiThieuCongTyViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }
	}
}